var express = require('express')
var router = express.Router()
const { getALL, getOne, exec } = require('../../db')

// 查询所有事情
router.get('/', async (req, res) => {
  let sql = 'select * from event GROUP BY event_time DESC'
  console.log(sql)
  const data = await getALL(sql)
  try {
    res.send({
      code: '200',
      msg: '获取所有事件成功',
      result: data,
    })
  } catch {
    res.send({
      code: '400',
      msg: '获取所有事件失败',
    })
  }
})

// 通过事件
router.post('/pass', async (req, res) => {
  const { id } = req.query
  let sql = `UPDATE event SET event_status= '1'  where id ='${id}'`
  await exec(sql)
  try {
    res.send({
      code: '200',
      msg: '审核成功',
    })
  } catch {
    res.send({
      code: '400',
      msg: '审核失败',
    })
  }
})

// 忽略事件
router.post('/ignore', async (req, res) => {
  const { id } = req.query
  let sql = `DELETE FROM event where id ='${id}'`
  await exec(sql)
  try {
    res.send({
      code: '200',
      msg: '忽略成功',
    })
  } catch {
    res.send({
      code: '400',
      msg: '忽略失败',
    })
  }
})

// 搜索事件
router.post('/search', async (req, res) => {
  const { value } = req.query
  // 多字段模糊查询
  let sql = `SELECT * FROM event WHERE CONCAT(event_type,event_address,event_mark,event_describe) LIKE '%${value}%'`
  const data = await getALL(sql)
  try {
    res.send({
      code: '200',
      msg: '查询成功',
      result: data,
    })
  } catch {
    res.send({
      code: '400',
      msg: '查询失败',
    })
  }
})

module.exports = router
